import { Navigate, useLocation } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
import { useLocalizedLink } from '@kernelproxy/i18n';

interface ProtectedRouteProps {
  children: React.ReactNode;
}

const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
  const { isAuthenticated, loading } = useAuth();
  const { getLink } = useLocalizedLink();
  const location = useLocation();

  if (loading) {
    return (
      <div className="h-screen bg-gray-50 dark:bg-gray-900 flex items-center justify-center">
        <div className="inline-block animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-[#00d4ff]"></div>
      </div>
    );
  }

  if (!isAuthenticated) {
    return <Navigate to={getLink('/login')} state={{ from: location }} replace />;
  }

  return <>{children}</>;
};

export default ProtectedRoute;
